package com.dao.impl;

import com.dao.Dao;
import com.pojo.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;
@Repository("daoImpl")
public class DaoImpl implements Dao {
    @Autowired
    private QueryRunner queryRunner;

    @Override
    public int findAll() {
        try {
           Number number= (Number) queryRunner.query("select count(1) from books",new ScalarHandler());
            return number.intValue();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Account findById(Integer id) {
        try {
            System.out.println("id:"+id);
            return queryRunner.query("select *from User where id=?", new BeanHandler<Account>(Account.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void saveAccount(Account account) {
        try {
            queryRunner.update("insert into User (id,userName,userSex,userAge,userDid) values (?,?,?,?,?)", account.getId(), account.getUserName(), account.getUserSex(), account.getUserAge(), account.getUserDid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(Account account) {

    }

    @Override
    public void delete(Integer id) {

    }

    public void setQueryRunner(QueryRunner queryRunner) {
        this.queryRunner=queryRunner;
    }
}
